<?php
/**
 * 控制台首页、系统设置、修改密码
 */
namespace app\Admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;

//后台首页继承自登陆判断页面，每次加载都会先执行判断页面的代码
class Index extends Basic
{
    // 控制台首页
    public function index()
    {
        $data['admin'] = session('admin_username');

        $request = Request::instance();
        // 获取IP
        $data['ip'] = $request->ip();
        // 获取当前域名
        $data['domain'] = $request->domain();
        // 获取当前操作系统
        $data['system'] = PHP_OS;
        // 运行环境
        $data['operating_environment'] = $_SERVER['SERVER_SOFTWARE'];
        // print_r($data);die();
        $this->assign('data',$data);
        $this->assign('title', '后台管理首页');
        return view('index');
    }
    // 系统设置
    public function system()
    {
        //系统信息
        $info = Db::name('system')->where('id',1)->find();
        $info['updatetime'] = date('Y-m-d H:i:s', $info['updatetime']);

        if (Request::instance()->isPost()){
            $id = input('post.id/d');
            $url = input('post.url');
            $title = input('post.title');
            $keywords = input('post.keywords');
            $desc = input('post.desc');

            if(empty($id)){
                $this->error('id参数错误！');
            }
            if(empty($url)){
                $this->error('域名不能为空！');
            }
            if(empty($title)){
                $this->error('网站名称不能为空！');
            }
            $data = array(
                'id'         => $id,
                'url'        => trim($url),
                'title'      => trim($title),
                'keywords'   => trim($keywords),
                'desc'       => trim($desc),
                'updatetime' => time(),
            );
            // print_r($data);die();
            $result = DB::name('system')->update($data);
            if(!$result) {
                $this->error('更新失败！',url('Index/index'));
            }else{
                $this->success('更新成功！',url('Index/index'));
            }
            return;
        }

        $this->assign('info', $info);
        $this->assign('title', '系统设置');

        return $this->fetch();
    }
    // 修改密码
    public function pwd(){
        $info = Db::name('admin')->where('id',1)->find();
        $info['updatetime'] = date('Y-m-d H:i:s', $info['updatetime']);
        if (Request::instance()->isPost()){
            $id = input('post.id/d');
            $password   = input('post.password');
            $repassword = input('post.repassword');
            if($password !== $repassword){
                $this->error('更新失败！',url('Index/pwd'));
            }
            $password = md5(trim($password));
            $data = array(
                'id'             => $id,
                'admin_password' => $password,
                'updatetime'     => time(),
            );
            $result = DB::name('admin')->update($data);
            if($result) {
                $this->success('更新成功！',url('Index/index'));
            }else{
                $this->error('更新失败！',url('Index/index'));
            }
            return;
        }
        // print_r($info);die();
        $this->assign('info', $info);
        $this->assign('title', '修改密码');
        return $this->fetch();
    }
}